package com.xsherl.leetcode.solution;

import com.xsherl.leetcode.utils.PrintUtils;

public class PlusOne {

    public int[] plusOne(int[] digits) {
        int len = digits.length, i = len - 1;
        digits[i] += 1;
        while (i > 0 && digits[i] >= 10){
            int m = digits[i];
            digits[i] = m - 10;
            digits[i - 1] += 1;
            i--;
        }
        if (digits[0] >= 10){
            int[] ints = new int[len + 1];
            ints[0] = 1;
            ints[1] = digits[0] - 10;
            for (int j = 2; j < ints.length; ++j){
                ints[j] = digits[j - 1];
            }
            return ints;
        }
        return digits;
    }

    public static void main(String[] args) {
        int[] d = {9, 9, 9};
        int[] ints = new PlusOne().plusOne(d);
        PrintUtils.println(ints);
    }
}
